package com.inspur.security.cbb3.kms.keyusage.data.service.impl;

import com.inspur.security.cbb3.kms.exception.UserInfoInVaildException;
import com.inspur.security.cbb3.kms.keyusage.data.po.DataBean;
import com.inspur.security.cbb3.kms.keyusage.data.repository.DataRepository;
import com.inspur.security.cbb3.kms.keyusage.data.service.DataService;
import com.inspur.security.cbb3.kms.sysuser.po.SysUserBean;
import com.inspur.security.cbb3.kms.sysuser.service.SysUserService;
import com.inspur.security.cbb3.kms.utils.DateUtil;
import javassist.NotFoundException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.MissingServletRequestParameterException;

import javax.transaction.Transactional;
import java.text.ParseException;
import java.util.List;
import java.util.Optional;

/**
 * DataServiceImpl
 *
 * @author liukai
 * @date 2020/10/20
 **/
@Service
@Slf4j
@Transactional
public class DataServiceImpl implements DataService {

    @Autowired
    private DataRepository dataRepository;

    @Autowired
    private SysUserService sysUserService;

    @Override
    public DataBean downData(String uuid) throws MissingServletRequestParameterException, NotFoundException {
        //参数校验
        if (StringUtils.isEmpty(uuid)) {
            throw new MissingServletRequestParameterException("主键字段uuid", "String");
        }
        //权限校验
        return checkPermission(uuid);
    }

    @Override
    public void deleteData(String uuid) throws MissingServletRequestParameterException, NotFoundException {
        //参数校验
        if (StringUtils.isEmpty(uuid)) {
            throw new MissingServletRequestParameterException("主键字段uuid", "String");
        }
        //权限校验
        checkPermission(uuid);
        //根据uuid删除数据
        dataRepository.deleteById(uuid);
    }

    /**
     * 校验登录用户是否有数据的操作权限
     *
     * @param uuid 数据id
     * @return 数据对象
     * @throws MissingServletRequestParameterException 异常信息-主键参数必填
     * @throws NotFoundException                       异常信息-未找到对象
     */
    private DataBean checkPermission(String uuid) throws MissingServletRequestParameterException, NotFoundException {
        if (StringUtils.isBlank(uuid)) {
            throw new MissingServletRequestParameterException("主键字段uuid", "String");
        }
        SysUserBean loginUser = sysUserService.findLoginSysUser();
        if (loginUser == null) {
            throw new UserInfoInVaildException();
        }
        Optional<DataBean> optional = dataRepository.findById(uuid);
        if (!optional.isPresent()) {
            throw new NotFoundException("对象不存在");
        }
        DataBean bean = optional.get();
        if (!loginUser.getUuid().equalsIgnoreCase(bean.getCreatorId())) {
            throw new NotFoundException("对象不存在");
        }
        return bean;
    }
}
